﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GenCodeTask
{
    public partial class Form1 : Form
    {
        GenCodeBiz genCodeBiz = new GenCodeBiz();
        int EnterpriseId = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                this.timer1.Stop();

                Queue<string> queue = genCodeBiz.GetGenCodeTask();
                if (queue.Count > 0)
                {
                    for (int i = 0; i < queue.Count; i++)
                    {
                        string[] genCode = queue.Dequeue().Split('-');
                        int genCodeTaskId = PageValidate.ConvertToTrimInt(genCode[0]);
                        string CodeType = genCode[1];//生码类型 集合
                        EnterpriseId = PageValidate.ConvertToTrimInt(genCode[2]);
                        int number = PageValidate.ConvertToTrimInt(genCode[3]);
                        string batch = genCode[4];
                       
                        int fwcodeNum = PageValidate.ConvertToTrimInt(genCode[5]);
                        int wlcodeNum = PageValidate.ConvertToTrimInt(genCode[6]);

                        int rankNum = PageValidate.ConvertToTrimInt(genCode[7]);
                        int VertialNum = PageValidate.ConvertToTrimInt(genCode[8]);
                        int ActiveType = PageValidate.ConvertToTrimInt(genCode[9]);
                        DateTime DisableTime = PageValidate.ConvertToDateTime(genCode[10]);
                        string companyCode = PageValidate.ConvertToTrimString(genCode[11]);
                        bool result = GenCode(CodeType, companyCode, number, batch, fwcodeNum, wlcodeNum, EnterpriseId, rankNum, VertialNum, ActiveType, DisableTime); //执行生码（创建数字码表并生码，生码成功改变生码的状态，todo:生码长度）
                        if (result)
                        {
                            //修改生码任务状态
                            genCodeBiz.UpdateGenCodeTaskState(genCodeTaskId);

                        }
                        i--;//i根据队列自减
                        GC.Collect();
                    }

                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                this.timer1.Start();
            }
            
        }

        private bool GenCode(string flag, string companyCode, int number, string batch, int fwlength, int wllength, int companyIds, int rankNum, int VertialNum,int ActiveType,DateTime DisableTime)
        {
            companyCode = companyCode.PadLeft(4, '0');

            string companyCodeBatch = companyCode + batch;
            return genCodeBiz.CreateCode(flag, fwlength, wllength, number, companyCodeBatch, companyIds, rankNum, VertialNum, ActiveType, DisableTime);
        }

    }
}
